#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;

void TopK(int k = 1)
{
    // 前k大，建值为k的小堆，不加仿函数默认是建大堆
    priority_queue<int, vector<int>, greater<int>> pq;
    srand(time(NULL));
    int n;
    cout << "输入一共多少个数：";
    cin >> n;
    vector<int> v;
    
    if(n < k)
        throw "n < k";

    for(int i = 0; i < n; ++i)
    {
        int num = rand() % 100 + 1;
        pq.push(num);
        v.push_back(num);
        if(pq.size() > k)
            pq.pop();
    }

    cout << "随机生成的数据：" << endl;
    for(auto& e : v)
        cout << e << " ";

    vector<int> ans;
    while(!pq.empty())
    {
        ans.push_back(pq.top());
        pq.pop();
    }
    
    reverse(ans.begin(), ans.end());
    cout << endl << "最大的前k个为：" << endl;
    for(auto& e : ans)
        cout << e << " ";
    return;
}

int main()
{
    int k;
    cout << "k值为：" << endl;
    cin >> k;
    TopK(k);
    cout << endl;
    return 0;
}
